// pages/bus/stations/stations.js
var config = wx.getStorageSync('config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    busList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      "id": options.id,
      // "id": "47001256", // 测试数据
    })
    console.log(options.id)

    var that = this;
    wx.request({
      url: config.host+'mini/bus/busWithStation/' + this.data.id,
      success: function (res) {
        if (res.statusCode != 200) {
          console.log(res)
          return;
        }
        var data = res.data;
        console.log(data);
        that.setData({
          "busList": data.list
        })
        // console.log(that.data)
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },

  toLine: function (e) {
    console.log(e)
    console.log(wx.getStorageSync("historyLines"))
    let historyLines = JSON.parse(wx.getStorageSync("historyLines"));

    var busId = e.currentTarget.dataset.busid;
    var busName = e.currentTarget.dataset.busname;

    historyLines.push({
      "id": busId,
      "name": busName
    })

    const arrayUnique = (arr, name) => {
      const hash = {};
      return arr.reduce((item, next) => {
        hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
        return item;
      }, []);
    };
    let lines = arrayUnique(historyLines, 'id');
    console.log(lines)
    this.setData({
      "historyLines": lines.reverse()
    })
    wx.setStorageSync('historyLines', JSON.stringify(lines.reverse()));

    wx.navigateTo({
      url: '/pages/bus/line/line?id=' + busId+"&name="+busName,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})